<?php

namespace Rblog\Module\Admin\Controller;

class Useradd extends Index {

    public function __construct() {
        $this->_working = 'useradd';
        parent::__construct();
    }

    public function show() {
        $this->view->display();
    }

    /**
     * 添加管理用户
     * 
     * @throws mixed 确认密码不正确或用户名已存在
     */
    public function add() {

        $post = $this->request->allInput('p');
        $user = \Rexzy::model('User');

        $name = array(
            'val' => $post['name'],
            'message' => '请输入用户名',
            'rules' => array(
                array(
                    'rule' => function($val) {
                        if (( strlen($val) < 4 ) || ( strlen($val) > 16 )) {
                            return false;
                        }
                    },
                    'error' => '请输入4-16个字符的用户名'
                ),
                array(
                    'rule' => function($val)use( $user ) {
                        $userid = $user->userid($val);
                        if ($userid) {
                            return false;
                        }
                    },
                    'error' => '用户名已存在'
                )
            )
        );

        $password = array(
            'val' => $post['password'],
            'message' => '请输入密码',
            'rules' => array(
                array(
                    'rule' => function($val) {
                        if (( strlen($val) < 6 ) || ( strlen($val) > 20 )) {
                            return false;
                        }
                    },
                    'error' => '请输入6-20个字符的密码'
                )
            )
        );

        $confPassword = array(
            'val' => $post['confPassword'],
            'message' => '请输入密码',
            'rules' => array(
                array(
                    'rule' => function( $val)use( $password ) {
                        if ($val !== $password['val']) {
                            return false;
                        }
                    },
                    'error' => "密码不一致"
                )
            )
        );

        if ($post['screenName'] == '') {
            $screenNameVal = $post['name'];
        } else {
            $screenNameVal = $post['screenName'];
        }

        $screenName = array(
            'val' => $screenNameVal,
            'require' => false
        );

        $mail = array(
            'val' => $post['mail'],
            'require' => false
        );

        $group = array(
            'val' => $post['group'],
            'message' => '请选择用户组',
            'rules' => array(
                array(
                    'rule' => function($val) {
                        $value = array( 'subscriber', "contributor", "editor", "administrator");
                        if( ! in_array( $val, $value) ) {
                            return false;
                        }
                    },
                    'error' => '请根据给出的选项选择用户组'
                )
            )
        );
        
        $data = array(
            'name' => $name,
            'password' => $password,
            'confPassword' => $confPassword,
            'screenName' => $screenName,
            'mail' => $mail,
            'group' => $group
        );

        $rightHandle = function( $rightData )use( $user ) {
            return $user->add($rightData['name'], md5($rightData['password'] . \Rexzy::getConfig('slat')), $rightData['screenName'], $rightData['mail'], $rightData['group']);
        };

        echo json_encode($this->formCheck($data, $rightHandle));
    }

}
